package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.testapplication.local.MyService;

public class shang_Service extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shang_service);

        // 启动服务

    }

    // 启动服务
    public void startMyService(View view){
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        Toast.makeText(this, "start server", Toast.LENGTH_SHORT).show();
    }

    // 停止服务
    public void stopMyService(View v) {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
        Toast.makeText(this, "stop service", 0).show();
    }

    private ServiceConnection conn;
    //绑定服务
    public void bindMyService(View v) {
        Intent intent = new Intent(this, MyService.class);
        //创建连接对象
        if(conn==null) {
            conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.e("TAG", "onServiceConnected()");
                }

                @Override
                public void onServiceDisconnected(ComponentName componentName) {
                    Log.e("TAG", "onServiceDisconnected()");
                }
            };
            //绑定Service
            bindService(intent, conn, Context.BIND_AUTO_CREATE);

            Toast.makeText(this, "bind service", 0).show();
        } else {
            Toast.makeText(this, "已经bindservice", 0).show();
        }

    }
}